import {
  Body,
  Controller,
  Del,
  Get,
  Inject,
  Param,
  Post,
  Put
} from '@midwayjs/core'
import {
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiTags
} from '@midwayjs/swagger'
import { Validate } from '@midwayjs/validate'

import { Permission } from '@/decorator'
import { DeleteDTO } from '@/dto'

import { CreateMenuDTO, FindMenuListDTO, UpdateMenuDTO } from './menu.dto'
import { MenuService } from './menu.service'
import { GetMenuListVO, GetMenuTreeVO } from './menu.vo'

@ApiBearerAuth()
@ApiTags('系统/菜单接口')
@Controller('/system/menu')
export class MenuController {
  @Inject()
  menuService: MenuService

  @ApiOperation({ summary: '获取菜单列表' })
  @ApiResponse({ status: 200, type: GetMenuListVO })
  @Permission('system:menu:list')
  @Post('/list')
  @Validate()
  async getMenuList(@Body() dto: FindMenuListDTO) {
    return await this.menuService.handleFindList(dto)
  }

  @ApiOperation({ summary: '获取菜单树' })
  @ApiResponse({ status: 200, type: GetMenuTreeVO })
  @Get('/tree')
  async getMenuTree() {
    return await this.menuService.handleFindTree()
  }

  @ApiOperation({ summary: '新建菜单' })
  @Permission('system:menu:add')
  @Post('/create')
  @Validate()
  async createMenu(@Body() dto: CreateMenuDTO) {
    await this.menuService.handleCreate(dto)
  }

  @ApiOperation({ summary: '更新菜单' })
  @Permission('system:menu:edit')
  @Put('/update/:id')
  @Validate()
  async updateMenu(@Param('id') id: string, @Body() dto: UpdateMenuDTO) {
    await this.menuService.handleUpdate(id, dto)
  }

  @ApiOperation({ summary: '删除菜单' })
  @Permission('system:menu:remove')
  @Del('/delete')
  @Validate()
  async deleteMenu(@Body() dto: DeleteDTO) {
    await this.menuService.handleDelete(dto)
  }
}
